package co.fitstart.weapp.api.dao;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;

import co.fitstart.entity.coach.CoachSigninStatistic;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.course.schedule.SigninRecord;
import co.fitstart.entity.course.schedule.StudentSigninRecord;
import co.fitstart.entity.principal.Principal;

/**
 * 
 * @author dan
 *
 */
@Dao("hibernate")
public interface SigninDao {
    
    /**
     * 
     * @param courseSchedule
     * @return
     */
    @Query("FROM StudentSigninRecord WHERE courseSchedule = :courseSchedule")
    public StudentSigninRecord loadSigninRecord(@Variable("courseSchedule") CourseSchedule courseSchedule);

    /**
     * 
     * @param coach
     * @return
     */
    @Query("SELECT new Map(SUM(hour) AS hour,SUM(experienceHour) AS experienceHour,SUM(giveHour) AS giveHour) FROM CoachSigninStatistic WHERE principal = :coach")
    public Map<String, Object> count(@Variable("coach") Principal coach);

    /**
     * 日销课课时数，不含体验课及赠课
     * @param coach
     * @param courseDay
     * @return
     */
    @Query("SELECT SUM(hour) FROM CoachSigninRecord WHERE principal = :coach AND courseDescription NOT IN ('1V1体验课','1V1赠课') AND courseDay = :courseDay")
    public BigDecimal count(@Variable("coach") Principal coach, @Variable("courseDay") Date courseDay);

    /**
     * 
     * @param coach
     * @return
     */
    @Query("FROM CoachSigninStatistic WHERE principal = :coach ORDER BY month DESC")
    public List<CoachSigninStatistic> listCoachSigninStatistic(@Variable("coach") Principal coach);

    /**
     * 月销课记录，不含体验课及赠课
     * @param coach
     * @param begin
     * @param end
     * @return
     */
    @Query("FROM SigninRecord WHERE coach = :coach AND courseSchedule.occurDate BETWEEN :begin AND :end AND courseSchedule.course.experience = false AND courseSchedule.course.give = false AND disabled = false")
    public List<SigninRecord> listCoachSigninRecord(@Variable("coach") Principal coach, @Variable("begin") Date begin, @Variable("end") Date end);
}
